home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / dev / basic / SocketFuncs.lha / v1.0 / TCPFuncs.bb < prev    next >
Encoding:
Text File  |  1999-10-10  |  7.0 KB  |  250 lines

  1. ; This code has been modified so that bsdsocket.library now
  2. ; no longer needs to be set up in Blitz.
  3. ;
  4. ; Modifications v1.0
  5. ;
  6. ; Modifications made:
  7. ;
  8. ; All bsdsocket.library function calls replaced with their ASM
  9. ; equivalent.
  10. ; TCPFuncs must now open and close the bsdsocket.library itself.
  11. ; This is done in ConnectTCP{} and CloseTCP{}.
  12. ;
  13. ; Modifications by Roger Light <rogerlight@mindless.com> all other
  14. ; work done as stated below.
  15. ;
  16. ; If you have any problems with this code, I advised that you
  17. ; try the original TCPFuncs code to see if it is these modifications
  18. ; that are the problem. It may well be that these mods are at fault.
  19. ; Please don't contact me for support because I don't know much
  20. ; really and I will also have no access to an Amiga from the
  21. ; 11/10/99.
  22. ; If however you don't know what advantages this code gives you
  23. ; over the original TCPFuncs, but think you might need it, please
  24. ; feel free to contact me.
  25.  
  26. XINCLUDE TCPSocketIncs.bb2
  27.  
  28. ;-----------------------------------------------------------
  29. ; Standard Blitz TCP Functions V1.8 by Paul Burkey (c)1997-1998
  30. ; Compiled with help from Ercole Spiteri and Anton Reinauer
  31. ; Contact me at burkey@bigfoot.com
  32. ;-----------------------------------------------------------
  33. ;History
  34. ;-------
  35. ;<16.2.97> Version 1.8
  36. ;Added NLPrintTCP{} for easy send string with carrage return and newline.
  37. ;Removed need for 3rd Party libs (only bsdsocket.library needed)
  38. ;
  39. ;<24.12.97> Version 1.7
  40. ;ReadTCP{} Updated with extra safety and Speed
  41. ;
  42. ;<18.9.97> Version 1.6
  43. ;Added PrintTCP{}  for an easy "send string" command.
  44. ;Added NPrintTCP{} for easy send string with carrage return
  45. ;CheckTCP{} merged into the ConnectTCP{} function.
  46. ;
  47. ;---------------
  48. ; Function List
  49. ;---------------
  50. ;
  51. ;ReadTCP{}                       ; Similar to Edit$() - recives data via TCP connection
  52. ;ReadMemTCP{ReadAdd.l,MaxSize.l} ; Similar to ReamMem - recives data via TCP connection
  53. ;WriteTCP{ad.l,size.w}           ; Similar to WriteMem - sends data via TCP connection
  54. ;ConnectTCP{host$,port.w}        ; Connect to a remote machine (Full error checking)
  55. ;PrintTCP{text$}                 ; Similar to Print - sends data via TCP connection
  56. ;NPrintTCP{text$}                ; Similar to NPrint - sends data via TCP connection
  57. ;NLPrintTCP{text$}               ; Similar to Print+CR+LF - sends data via TCP connection
  58. ;CloseTCP{}                      ; Closes TCP Connection
  59.  
  60. ;---------------------------------
  61. ; TCP library Variables/Constants
  62. ;---------------------------------
  63.  
  64. #TCPBuflen=$2048                ;Maximum data size to read at any time
  65. TCPmem.l=AllocMem(#TCPBuflen,0) ;Allocate the temp buffer used for all TCP reads
  66. #FIONREAD=$4004667f             ;FIONREAD request
  67.  
  68. ;---------------------------------
  69. ; Standard TCP library structures
  70. ;---------------------------------
  71.  
  72. NEWTYPE.list
  73.   *ItemA.b
  74.   *ItemB.b
  75. End NEWTYPE
  76. NEWTYPE.inaddr
  77.   s_addr.l
  78. End NEWTYPE
  79. NEWTYPE.sockaddrin
  80.   sin_len.b
  81.   sin_family.b
  82.   sin_port.w
  83.   sin_addr.inaddr
  84.   sin_zero.b[8]
  85. End NEWTYPE
  86. NEWTYPE.hostent
  87.   *h_name.b
  88.   *h_aliases.list
  89.   h_addrtype.l
  90.   h_lenght.l
  91.   *h_addr_list.list
  92. End NEWTYPE
  93.  
  94. ;---------------------------------
  95. ; Standard TCP Blitz Functions
  96. ;---------------------------------
  97.  
  98. .ReadTCP
  99. Function .s ReadTCP{}
  100.   SHARED sock.l,TCPmem.l
  101.   ;
  102.   ; This Function reads data from the server the result is passed back in a
  103.   ; string. If there is no messages then it will return an empty string =""
  104.   ;
  105.   sockread.l=0                                ;Clear Readmask
  106.   sockread.l BitSet sock.l                    ;Set Readmask on our socket
  107.   e.l=IoctlSocket{sock.l,#FIONREAD,TCPmem.l}  ;How much data is there?
  108.   f.l=Peek.l(TCPmem.l)                        ;Place value in f
  109.   If f>0
  110.     If f>#TCPBuflen Then f=#TCPBuflen         ;Don't read more than #TCPBuflen
  111.     c=recv{sock.l,TCPmem.l,f,0}               ;Read all Data
  112.     c$=String$(" ",f)                         ;Reserve String
  113.     CopyMem_ TCPmem.l,&c$,f                   ;Copy Data to string
  114.     Function Return c$
  115.   Else
  116.     Function Return ""
  117.   EndIf
  118. End Function
  119.  
  120.  
  121. ;----
  122. ;WARNING: This is a 'rough' experiment function.
  123. ;Function will probably change next update.
  124. ;----
  125.  
  126. .ReadMemTCP
  127. Function .l ReadMemTCP{ReadAdd.l,MaxSize.l}
  128.   SHARED sock.l,TCPmem.l
  129.   ;
  130.   ; Read into memory location 'ReadAdd.l' up to a maximum of 'MaxSize.l'
  131.   ; Used for reading long binary files eg, WWW files or FTP files.
  132.   ; Also returns the amount of bytes actually read.
  133.   ;
  134.   sockread.l=0                                ;Clear Readmask
  135.   sockread.l BitSet sock.l                    ;Set Readmask on our socket
  136.   e.l=IoctlSocket{sock.l,#FIONREAD,TCPmem.l}  ;How much data is there?
  137.   f.l=Peek.l(TCPmem.l)                        ;Place value in f
  138.   If f>0
  139.     If f>#TCPBuflen Then f=#TCPBuflen         ;Don't read more than #TCPBuflen
  140.     If f>MaxSize Then f=MaxSize               ;Don't read more than MaxSize
  141.     c=recv{sock.l,ReadAdd.l,f,0}              ;Read Data to ReadAdd location
  142.     Function Return f
  143.   Else
  144.     Function Return 0
  145.   EndIf
  146.   ;
  147. End Function
  148.  
  149.  
  150.  
  151. .WriteMemTCP
  152. Statement WriteMemTCP{ad.l,size.w}
  153.   SHARED sock.l
  154.   ;
  155.   ; This routine writes data via TCP.
  156.   ;
  157.   sockwrite.l=0                           ;Clear Writemask
  158.   sockwrite.l BitSet sock.l               ;set Writemask on our socket
  159.   g.l=WaitSelect{2,0,&sockwrite.l,0,0,0}  ;Wait until server is ready to read our data
  160.   c.l=send{sock.l,ad,size,0}              ;Send data to server
  161. End Statement
  162.  
  163.  
  164.  
  165. .ConnectTCP
  166. Function .b ConnectTCP{host$,port.w}
  167.   SHARED sock.l
  168.   ;
  169.   ; Check if Miami/AmiTCP stack is available
  170.   ; Connect to host at specified port
  171.   ; Return true or False if Connection is made
  172.  
  173.   SocketBase.l=OpenLibrary_("bsdsocket.library",0)
  174.   If SocketBase=0
  175.     Function Return False
  176.   Else
  177. ;;    CloseLibrary_(SocketBase)
  178.     sock.l=socket{2,1,0}
  179.     *a.hostent=gethostbyname{host$}
  180.     If *a=0
  181.       Function Return False   ; host not found (or internal TCP error)
  182.     Else
  183.       ;
  184.       ; Copy Details to our Sockaddrin structure
  185.       ;
  186.       CopyMem_ *a\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a\h_lenght
  187.       host.sockaddrin\sin_port=port       ;Set port number
  188.       host.sockaddrin\sin_family=2        ;Set type to AT_INET
  189.       StructLength.l=SizeOf .sockaddrin   ;Get lenght of structure sockaddrin
  190.       If connect{sock.l,host.sockaddrin,StructLength}=-1
  191.         CloseSocket{sock.l}
  192.         Function Return False
  193.       Else
  194.         Function Return True
  195.       EndIf
  196.     EndIf
  197.   EndIf
  198. End Function
  199.  
  200.  
  201.  
  202. .PrintTCP
  203. Statement PrintTCP{text$}
  204.   ;
  205.   ; Send String via TCP
  206.   ;
  207.   WriteMemTCP{&text$,Len(text$)}
  208. End Statement
  209.  
  210.  
  211.  
  212. .NPrintTCP
  213. Statement NPrintTCP{text$}
  214.   ;
  215.   ; Send String via TCP + Carrage Return
  216.   ;
  217.   text$=text$+Chr$(13)
  218.   WriteMemTCP{&text$,Len(text$)}
  219. End Statement
  220.  
  221.  
  222.  
  223. .NLPrintTCP
  224. Statement NLPrintTCP{text$}
  225.   ;
  226.   ; Send String via TCP + Carrage Return + Line Feed
  227.   ;
  228.   text$=text$+Chr$(13)+Chr$(10)
  229.   WriteMemTCP{&text$,Len(text$)}
  230. End Statement
  231.  
  232.  
  233.  
  234. .CloseTCP
  235. Statement CloseTCP{}
  236.   SHARED sock.l
  237.   SHARED SocketBase.l
  238.   ;
  239.   ; This is a simple close socket command
  240.   ; Provided for the shear hell of it :)
  241.   ; Now also closes bsdsocket.library.
  242.   ;
  243.   CloseSocket{sock.l}
  244.   CloseLibrary_(SocketBase)
  245. End Statement
  246.  
  247.  
  248.  
  249.  
  250.